package com.siyoumi.app.test.rabbitmq;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Envelope;
import com.siyoumi.app.rabbitmq.RabbitMqConsumer;
import com.siyoumi.component.XApp;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

//延迟队列消费者
@Slf4j
public class RabbitMqConsumerDelay
        extends RabbitMqConsumer {
    @Override
    public void handleDelivery(String s, Envelope envelope, AMQP.BasicProperties basicProperties, byte[] body) throws IOException {
        String message = new String(body, StandardCharsets.UTF_8);
        log.debug("message: {}", message);

        int rnd = XApp.random(0, 10);
        if (rnd > 5) {
            log.debug("确认消息-成功");
            //参数2:[true]确认小于等于tag的所有消息;[false]确认当前的消息
            getChannel().basicAck(envelope.getDeliveryTag(), false);
        } else {
            log.debug("确认消息-失败-重发");
            //参数2:[true]重发;[false]丢弃
            getChannel().basicReject(envelope.getDeliveryTag(), true);
            //参数2:[true]拒绝小于等于tag的所有消息;[false]拒绝当前的消息
            //参数3:[true]重发;[false]丢弃
            //getChannel().basicNack(envelope.getDeliveryTag(), false, false);
        }
    }
}